<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=s, initial-scale=1.0">
    <title>Symbols</title>
</head>
<body>
    <script>
        // 原始数据类型
        /*
            1. undefined
            2. null
            3. Boolean
            4. string
            5. number
            6. Object
            7. Symbol
        */

        // Symbol: 产生唯一值的数据类型
        /*const PADDING = Symbol('PADDING')
        const FULFILLED = Symbol('FULFILLED')
        const REJECTED = Symbol('REJECTED')

        console.log(
            PADDING.toString()
        )*/

        // 用 symbol 作为对象的 key
        
        let each = Symbol('each')
        let util = {
            each: () => {
                console.log('util each')
            },
            indexOf: () => {

            }
        }
        util[each] = () => {
            console.log('Symbol each')
        }

        util[each]()
        util.each()

        // 捕捉对象中 symbol 类型的 key
        console.log(
            Object.getOwnPropertySymbols(util)
        )
        // 捕捉对象中不是 symbol 类型的 key
        console.log(
            Object.getOwnPropertyNames(util)
        )

        // 捕捉对象所有自定义的key
        console.log(
            Reflect.ownKeys(util)
        )

        let sleep = Symbol('sleep')

        class Person {
            constructor({ name, age }) {
                this.name = name
                this.age = age
            }
            [Symbol.hasInstance](v) {
                console.log('test', v)
                return true
            }
            [sleep]() {
                return console.log(`${this.name} 正在睡觉`)
            }
        }

        // console.log(
        //     new Person({ name: 'sxl' })[sleep]()
        // )
        let p = new Person({ name: 'sxl' })
        console.log(
            p instanceof Person
        )

        // 定义两个一模一样的Symbol 
        let str = Symbol('str')
        let str_1 = Symbol.for('str1')
        let str_2 = Symbol.for('str1')
        console.log(
            '相等的 Symbol :',
            str_1,
            str_1 === str_2
        )

        // Symbol() 接受对象
        let o = {
            toString: () => {
                return 'test o'
            }
        }
        console.log(
            Symbol(o)
        )

    </script>
</body>
</html>